<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24" v-has-role="['admin']">
        <el-card shadow="hover">
          <el-input v-model="deptName" placeholder="请输入部门名称" prefix-icon="Search" clearable/>
          <el-tree
              class="mt-2"
              ref="deptTreeRef"
              :data="deptOptions"
              :props="{ label: 'label', children: 'children' }"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              highlight-current
              default-expand-all
              @node-click="handleNodeClick"
          />
        </el-card>
      </el-col>
      <!--收发机数据-->
      <el-col :span="checkRole(['admin'])?20:24" :xs="24">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                    :leave-active-class="proxy?.animate.searchAnimate.leave">
          <div class="search" v-show="showSearch">
            <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
              <el-form-item label="用户名称" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable style="width: 240px"
                          @keyup.enter="handleQuery"/>
              </el-form-item>
              <el-form-item label="手机号码" prop="phonenumber">
                <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable style="width: 240px"
                          @keyup.enter="handleQuery"/>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="handleQuery" icon="Search">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </transition>

        <el-card shadow="hover">
          <template #header>
            <el-row :gutter="10">
              <el-col :span="1.5">
                <el-button type="primary" plain @click="handleAdd()" icon="Plus">新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="success" plain @click="handleUpdate()" :disabled="single"
                           v-has-permi="['system:user:add']" icon="Edit">
                  修改
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="danger" plain @click="handleDelete()" :disabled="multiple"
                           v-has-permi="['system:user:delete']" icon="Delete">
                  删除
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-dropdown class="mt-[1px]">
                  <el-button plain type="info">
                    更多
                    <el-icon class="el-icon--right">
                      <arrow-down/>
                    </el-icon
                    >
                  </el-button>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item @click="importTemplate" icon="Download">下载模板</el-dropdown-item>
                      <el-dropdown-item @click="handleImport" icon="Top"> 导入数据</el-dropdown-item>
                      <el-dropdown-item @click="handleExport" icon="Download"> 导出数据</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" :search="true"/>
            </el-row>
          </template>
        </el-card>
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" draggable append-to-body>
      <el-form :model="form" :rules="rules" label-width="100px" ref="gatewayRef">
        <el-row>
          <el-col :span="12">
            <el-form-item label="网络协议" prop="protocol">
              <el-select v-model="form.protocol" placeholder="请选择协议" size="default">
                <el-option v-for="item in equip_gateway_protocol" :key="item.value" :label="item.label"
                           :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="DTU_ID" prop="sbId">
              <el-input v-model="form.sbId"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收发机名称" prop="trName">
              <el-input v-model="form.trName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="探头容量" prop="probeNum">
              <el-input v-model="form.probeNum"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发射频率" prop="launch">
              <el-input v-model="form.launch"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接收功率" prop="power">
              <el-input v-model="form.power"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="上报周期" prop="cycle">
              <el-input v-model="form.cycle"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="deptId">
              <el-select v-model="form.deptId" placeholder="请选择部门" size="default" clearable @change="getUsers">
                <el-option v-for="item in deptDialogOptions" :key="item.value" :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属用户" prop="userId">
              <el-select v-model="form.userId" placeholder="请选择用户" size="default" clearable>
                <el-option v-for="item in userDialogOptions" :key="item.value" :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {addUser, deptSelect, deptTreeSelect, getUser, listOfdDept, updateUser} from "@/api/system/user";
import {checkRole} from "@/utils/permission";
import {listGateway} from "@/api/equip/gateway";
import {validateFloat63, validateInteger, validateLaunch, validateMQTT, validateProbeNum} from "@/utils/vaild-rule";

const {proxy} = getCurrentInstance();
const {equip_gateway_protocol} = proxy.useDict("equip_gateway_protocol");
const deptName = ref("");
const deptOptions = ref(undefined);
const deptDialogOptions = ref(undefined)
const userDialogOptions = ref(undefined)
const showSearch = ref(true);

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data
    if (checkRole(['admin'])) {
      deptDialogOptions.value = response.data[0].children.map(item => ({
        value: item.id,
        label: item.label
      }));
    } else {
      deptDialogOptions.value = response.data.map(item => ({value: item.id, label: item.label}))
    }
  });
};
const data = reactive({
  form: {},
  queryParams: {
    deptId: undefined
  },
  rules: {
    protocol: [{
      required: true,
      message: '请选择网络协议',
      trigger: 'blur'
    }],
    sbId: [
      {
        required: true,
        message: '请输入DTU ID',
        trigger: 'blur'
      },
      {
        validator: checkSbId, trigger: 'blur'
      }
    ],
    trName: [
      {
        required: true,
        message: '请输入收发机名称',
        trigger: 'blur'
      }
    ],
    probeNum: [
      {
        required: true,
        message: '请输入探头数量',
        trigger: 'blur'
      },
      {
        validator: validateProbeNum, trigger: 'blur'
      }
    ],
    launch: [
      {
        required: true,
        message: '请输入发射频率',
        trigger: 'blur'
      }, {
        validator: validateLaunch, trigger: 'blur'
      }
    ],
    power: [
      {
        required: true,
        message: '请输入接收功率',
        trigger: 'blur'
      }, {
        validator: validateFloat63, trigger: 'blur'
      }
    ],
    cycle: [
      {
        required: true,
        message: '请输入上报周期',
        trigger: 'blur'
      },
      {
        validator: validateInteger, trigger: 'blur'
      }
    ],
    deptId: [
      {
        required: true,
        message: '请绑定用户组',
        trigger: 'blur'
      }
    ],
    userId: [
      {
        required: true,
        message: '请选择用户',
        trigger: 'blur'
      }
    ]
  }
});

const {queryParams, form, rules} = toRefs(data);

/**
 校验SbId规则
 **/
function checkSbId(rule, value, callback) {
  if (form.value.protocol == 2) {
    const regex = /^[a-zA-Z0-9]{12}$/;// 任意12位数字或字符
    if (!regex.test(value)) {
      callback(new Error('请输入正确MAC'));
    } else {
      callback();
    }
  } else {
    const regex = /^[a-zA-Z0-9]{15}$/;// 任意12位数字或字符
    if (!regex.test(value)) {
      callback(new Error('请输入正确DTU_ID'));
    } else {
      callback();
    }
  }
}


const title = ref("");
const open = ref(false);

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
};

/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  title.value = "添加用户";
};


/** 查询收发机列表 */
function getList() {
  listGateway().then(res => {
    console.log(res)
  });
};

function getUsers() {
  form.value.userId = undefined
  if (form.value.deptId)
    listOfdDept(form.value.deptId).then(res => {
      userDialogOptions.value = res.data
    })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["gatewayRef"].validate(valid => {
  });
};

getDeptTree();
getList();
</script>

<style lang="scss" scoped>
</style>
